package business.util;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class PropriedadeExtraTexto extends PlainDocument 
{
	private static final long serialVersionUID = 5109092850532079051L;
	private int maxLength;
	private boolean maiusculo;
	
	/**
	 * 
	 * @param maxLength
	 * @param maiusculo
	 */
	public PropriedadeExtraTexto(int maxLength, boolean maiusculo)
	{
		super();
		this.maxLength = maxLength;
		this.maiusculo = maiusculo;
	}
	
	/**
	 * 
	 */
	public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
	{
		int length;
		
		if (str == null)
			return;
		
		if(maiusculo)
		{
			str = str.toUpperCase();
		}
		
		if(maxLength > 0)
		{		
			length = (getLength() + str.length());
			
			if(length > maxLength)
			{				
				str = str.substring(0, (maxLength - getLength()));
			}
		}
		
		super.insertString(offs, str, a);
	}
}
